///*
// * Licensed to the Apache Software Foundation (ASF) under one
// * or more contributor license agreements.  See the NOTICE file
// * distributed with this work for additional information
// * regarding copyright ownership.  The ASF licenses this file
// * to you under the Apache License, Version 2.0 (the
// * "License"); you may not use this file except in compliance
// * with the License.  You may obtain a copy of the License at
// *
// *     http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//
//package com.bff.gaia.mix.connectors.jdbc;
//
//import com.bff.gaia.annotation.VisibleForTesting;
//import com.bff.gaia.api.common.typeinfo.TypeInformation;
//import com.bff.gaia.api.java.DataSet;
//import com.bff.gaia.api.java.typeutils.RowTypeInfo;
//import com.bff.gaia.mix.api.datastream.DataMixStream;
//import com.bff.gaia.table.sinks.AppendStreamTableSink;
//import com.bff.gaia.table.sinks.BatchTableSink;
//import com.bff.gaia.table.sinks.TableSink;
//import com.bff.gaia.types.Row;
//import com.bff.gaia.util.InstantiationUtil;
//import com.bff.gaia.util.Preconditions;
//
//import java.io.IOException;
//import java.util.stream.Collectors;
//import java.util.stream.IntStream;
//import java.util.stream.Stream;
//
///**
// * An at-least-once Table sink for JDBC.
// *
// * <p>The mechanisms of Gaia guarantees delivering messages at-least-once to this sink (if
// * checkpointing is enabled). However, one common use case is to run idempotent queries
// * (e.g., <code>REPLACE</code> or <code>INSERT OVERWRITE</code>) to upsert into the database and
// * achieve exactly-once semantic.</p>
// */
//public class JDBCAppendTableSink implements AppendStreamTableSink<Row>, BatchTableSink<Row> {
//
//	private final JDBCOutputFormat outputFormat;
//
//	private String[] fieldNames;
//	private TypeInformation[] fieldTypes;
//
//	JDBCAppendTableSink(JDBCOutputFormat outputFormat) {
//		this.outputFormat = outputFormat;
//	}
//
//	public static JDBCAppendTableSinkBuilder builder() {
//		return new JDBCAppendTableSinkBuilder();
//	}
//
//	@Override
//	public void emitDataStream(DataStream<Row> dataStream) {
//		dataStream.addSink(new JDBCSinkFunction(outputFormat));
//	}
//
//	@Override
//	public void emitDataSet(DataSet<Row> dataSet) {
//		dataSet.output(outputFormat);
//	}
//
//	@Override
//	public TypeInformation<Row> getOutputType() {
//		return new RowTypeInfo(fieldTypes, fieldNames);
//	}
//
//	@Override
//	public String[] getFieldNames() {
//		return fieldNames;
//	}
//
//	@Override
//	public TypeInformation<?>[] getFieldTypes() {
//		return fieldTypes;
//	}
//
//	@Override
//	public TableSink<Row> configure(String[] fieldNames, TypeInformation<?>[] fieldTypes) {
//		int[] types = outputFormat.getTypesArray();
//
//		String sinkSchema =
//			String.join(", ", IntStream.of(types).mapToObj(JDBCTypeUtil::getTypeName).collect(Collectors.toList()));
//		String tableSchema =
//			String.join(", ", Stream.of(fieldTypes).map(JDBCTypeUtil::getTypeName).collect(Collectors.toList()));
//		String msg = String.format("Schema of output table is incompatible with JDBCAppendTableSink schema. " +
//			"Table schema: [%s], sink schema: [%s]", tableSchema, sinkSchema);
//
//		Preconditions.checkArgument(fieldTypes.length == types.length, msg);
//		for (int i = 0; i < types.length; ++i) {
//			Preconditions.checkArgument(
//				JDBCTypeUtil.typeInformationToSqlType(fieldTypes[i]) == types[i],
//				msg);
//		}
//
//		JDBCAppendTableSink copy;
//		try {
//			copy = new JDBCAppendTableSink(InstantiationUtil.clone(outputFormat));
//		} catch (IOException | ClassNotFoundException e) {
//			throw new RuntimeException(e);
//		}
//
//		copy.fieldNames = fieldNames;
//		copy.fieldTypes = fieldTypes;
//		return copy;
//	}
//
//	@VisibleForTesting
//	JDBCOutputFormat getOutputFormat() {
//		return outputFormat;
//	}
//}